$(function(){
	/**调用初始化方法*/
	/*getAllRole();*/
	queryRole(0,0,"",10);
});
function getAllRole(){
	$.post("/UserGetAllRoleWorker/",null,function(str){
		str = decodeURI(str);
		var data = $.parseJSON(str);
		for(var i=0;i<data.length;i++){
			$("#selectId").append("<option value='"+data[i].RoleId+"'>"+data[i].RoleName+"</option>");
		}
	});
}
function queryRole(startTime,endTime,contain,method){
	$(this).hide();
	$('#usertable').bootstrapTable('destroy');
	$('#usertable').bootstrapTable({
		method: 'get',
		url: '/GetAllBuyProp/?startTime='+startTime+'&endTime='+endTime+'&contain='+contain+'&method='+method,
		cache: false,
		striped: true,
		pagination:true,
		pageSize:20,
		pageList: [5, 10, 20,50],
		pageNumber:1,
		sidePagination:'server',
		height:802,
		width:1600,
		paginationPreText: "上一页",
		paginationNextText: "下一页",     
		paginationHAlign: "left",  
		paginationDetailHAlign: "right", 
		columns: [
			{
				field: 'xuhao',
				title: '序号',
				align: 'center',
				formatter: turnAround,
				valign: 'middle',
			}, {
				field: 'propName',
				title: '道具名称',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			}, {
				field: 'id',
				title: '使用ID',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			}, {
				field: 'propAccountid',
				title: '使用账号',
				formatter: returnValue,
				align: 'center',
				valign: 'middle'
			},{
				field: 'receiveId',
				title: '接受ID',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			},{
				field: 'useAccountId',
				title: '接受账号',
				formatter: returnValue1,
				align: 'center',
				valign: 'middle'
			},{
				field: 'game',
				title: '赠送前身上',
				align: 'center',
				valign: 'middle'
			},{
				field: 'room',
				title: '赠送前银行',
				align: 'center',
				valign: 'middle'
			},{
				field: 'price',
				title: '价格',
				formatter: turnAround,
				align: 'center',
				valign: 'middle'
			},{
				field: 'buyNum',
				title: '购买数量',
				align: 'center',
				valign: 'middle'
			},{
				field: 'time',
				title: '时间',
				align: 'center',
				valign: 'middle'
			},{
				field: 'ipString',
				title: 'IP地址',
				align: 'center',
				valign: 'middle'
			}]
	});
}
/**
 * table图标区
 */
function operateFormatter(value, row, index) {
	return [
		'<button class="updateUser" id="updateUser">修改</button>'
		].join('');
}
function returnValue(value,row,index){
	var hideSave=0;
	return "<a  onclick='openNewBlank("+row.id+","+hideSave+")'>"+value+"</a>";
}
function returnValue1(value,row,index){
	var hideSave=0;
	return "<a  onclick='openNewBlank("+row.receiveId+","+hideSave+")'>"+value+"</a>";
}

/**
 * 冻结管理员
 * @returns
 */
$(".clockUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerClockUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
/**
 * 解冻管理员
 * @returns
 */
$(".dclockUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerDClockUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
/**
 * 删除管理员
 * @returns
 */
$(".deleteUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerDeleteUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
function getUserTableCheckBoxIds(){
	var a=$('#usertable').bootstrapTable('getSelections');
	var ids="";
	if(a.length<1){
		alert("请至少选择一行操作");
	}else{
		var len=a.length;
		ids=a[0].uid;
		for(var i=1;i<len;i++){
			ids+=(","+a[i].uid);
		}
		return ids;
	}
}
/**
 * 删除角色
 * @returns
 */
$(".deleteRole").click(function(){
	var roleids=getRoleTableCheckBoxIds();
	alert(roleids);
	$.post("/deleteRole/",{"roleids":roleids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
function getRoleTableCheckBoxIds(){
	var a=$('#Roletable').bootstrapTable('getSelections');
	var ids="";
	if(a.length<1){
		alert("请至少选择一行操作");
	}else{
		var len=a.length;
		ids=a[0].roleId;
		for(var i=1;i<len;i++){
			ids+=(","+a[i].roleId);
		}
		return ids;
	}
}
/**
 * 图标绑定点击事件
 */
window.operateEvents = {
		'click .updateUser': function (e, value, row, index) {
			isModify=1;
			$("#titleId").html("修改用户");
			$(".username").val(decodeURI(row.uname,"utf-8"));
			$(".password").val(decodeURI(row.upassword,"utf-8"));
			$("#selectId").val(row.roleid);
			$("#userIds").val(row.uid);
			$("#show_div_user").show();
		}
};

function turnAround(value, row, index){
	return decodeURI(value,"utf-8"); 
}



/* 点击提交的事件  */
$("#addUser").click(function(){
    $(".show_div").hide();
    var data={};
    var userName=$(".username").val();
    var chinaName=$(".password").val();
    var roleSId=$("#selectId").val();
    data.userName = userName;
    data.password = chinaName;
    data.roleSId = roleSId;
    var userid = $("#userIds").val();
    if(userid!=0 && userid!=null){
    	data.userid = userid;
    }
    if(userName && chinaName  && roleSId){
    	$(this).html('发送中...');
        $(this).css({"background":"#616161"});
        $.get("/UserAddAndUpdateWorker/",data,function(data){
        	data = decodeURI(data);
	    	var data1 = $.parseJSON(data);
	    	if(data1.state == 1){
	    		window.location.reload();
	    		alert(data1.message);
	    	}else{
	    		$(".showList_submit").html('提交');
	    		$(".showList_submit").css({"background":"#438EB9"});
	    		alert(data1.message);
	    		$("#show_div_user").show();
	    	}
        });
    }else{
        alert("填写错误");
    }
});
